package com.boilermaker.lotterysystem.common.interceptor;

import com.boilermaker.lotterysystem.common.utils.JWTUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

        String jwt = request.getHeader("user_token");
        log.debug("从 {} 中获取 JWT 令牌", request.getRequestURI());

        Claims claims = JWTUtil.parseJWT(jwt);

        if (claims == null) {
            response.setStatus(401);
            return false;
        }

        log.debug("解析 JWT 令牌成功");
        return true;
    }
}
